package cn.tedu.charging.order.amqp;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
public class AmqpProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //专门发送延迟消息的方法
    //目标交换机名称,消息内容(消息体),延迟时间
    public void sendDelay(String exchange,String message,Integer expiration){
        //1.组织消息
        byte[] body=message.getBytes(StandardCharsets.UTF_8);
        //2.消息属性
        MessageProperties properties=new MessageProperties();
        properties.setExpiration(expiration.toString());
        //3.组织消息
        Message msg=new Message(body,properties);
        rabbitTemplate.send(exchange,null,msg);
    }
}
